import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_app/Const/IFinal.dart';
import 'package:flutter_app/View/MySupportViews.dart';
import 'package:flutter_yfree/YFree.dart';

class MyLoader extends StatefulWidget {
  const MyLoader({Key? key}) : super(key: key);

  @override
  _MyLoaderState createState() => _MyLoaderState();
}

class _MyLoaderState extends State<MyLoader> with SingleTickerProviderStateMixin {
  late AnimationController animationController;
  late Animation<double> animationRotation;
  late Animation<double> animationRadiusIn;
  late Animation<double> animationRadiusOut;
  final double _initialRadius = 30.0;
  double _radius = 0;

  @override
  void initState() {
    super.initState();
    animationController = AnimationController(vsync: this, duration: const Duration(seconds: 3));
    animationRotation = Tween<double>(
      begin: 0.0,
      end: 1.0,
    ).animate(CurvedAnimation(parent: animationController, curve: const Interval(0.0, 1.0, curve: Curves.linear)));
    animationRadiusIn = Tween<double>(
      begin: 1.0,
      end: 0.0,
    ).animate(CurvedAnimation(parent: animationController, curve: const Interval(0.75, 1.0, curve: Curves.elasticIn)));
    animationRadiusOut = Tween<double>(
      begin: 0.0,
      end: 1.0,
    ).animate(CurvedAnimation(parent: animationController, curve: const Interval(0.0, 0.25, curve: Curves.elasticOut)));
    animationController.addListener(() {
      setState(() {
        if (animationController.value >= 0.75 && animationController.value <= 1.0) {
          _radius = animationRadiusIn.value * _initialRadius;
        } else if (animationController.value >= 0.0 && animationController.value < 0.25) {
          _radius = animationRadiusOut.value * _initialRadius;
        }
      });
    });
    animationController.repeat();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: SizedBox(
        width: 100.0,
        height: 100.0,
        child: Center(
          child: RotationTransition(
            turns: animationRotation,
            child: Stack(
              children: <Widget>[
                // _Dot(radius: 30.0, color: YConfig.themeColor),
                Center(
                  child: Container(
                    width: 30,
                    height: 30,
                    decoration: MyBoxDecoration(
                      borderRadius: BorderRadius.circular(99),
                      gradient: IFinal.Colors.btnThemeGradient,
                    ),
                  ),
                ),
                Transform.translate(
                  offset: Offset(_radius * cos(pi / 4), _radius * sin(pi / 4)),
                  child: const _Dot(radius: 5.0, color: Colors.redAccent),
                ),
                Transform.translate(
                  offset: Offset(_radius * cos(2 * pi / 4), _radius * sin(2 * pi / 4)),
                  child: const _Dot(radius: 5.0, color: Colors.orange),
                ),
                Transform.translate(
                  offset: Offset(_radius * cos(3 * pi / 4), _radius * sin(3 * pi / 4)),
                  child: const _Dot(radius: 5.0, color: Colors.yellow),
                ),
                Transform.translate(
                  offset: Offset(_radius * cos(4 * pi / 4), _radius * sin(4 * pi / 4)),
                  child: const _Dot(radius: 5.0, color: Colors.green),
                ),
                Transform.translate(
                  offset: Offset(_radius * cos(5 * pi / 4), _radius * sin(5 * pi / 4)),
                  child: const _Dot(radius: 5.0, color: Colors.cyan),
                ),
                Transform.translate(
                  offset: Offset(_radius * cos(6 * pi / 4), _radius * sin(6 * pi / 4)),
                  child: const _Dot(radius: 5.0, color: Colors.blue),
                ),
                Transform.translate(
                  offset: Offset(_radius * cos(7 * pi / 4), _radius * sin(7 * pi / 4)),
                  child: const _Dot(radius: 5.0, color: Colors.purple),
                ),
                Transform.translate(
                  offset: Offset(_radius * cos(8 * pi / 4), _radius * sin(8 * pi / 4)),
                  child: const _Dot(radius: 5.0, color: Colors.pink),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }
}

class _Dot extends StatelessWidget {
  final double radius;
  final Color color;

  const _Dot({Key? key, required this.radius, required this.color}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: radius,
        height: radius,
        decoration: BoxDecoration(color: color, shape: BoxShape.circle),
      ),
    );
  }
}
